import { User } from "../../../../modeal/user.js"
import { MyDate } from "../../../../utils/util.js"
const user = new User()
Page({
  data: {
    oid: '',
    orderDetail: {},
    postTime: '',
    polyline: [{
      points: [],
      color: '#4c7b6b',
      width: 3,
      dottedLine: true
    }],
    myLocation: {},
    duration: '', //预计骑行时间
    distance: '', //骑行距离
  },
  onLoad: function (options) {
    this.setData({ oid: options.oid })
  },

  onShow: function () {
    let oid = this.data.oid
    this.getOrderDetail(oid);  // 获取订单详情
  },


  getOrderDetail (oid) {
    user.getOrderDetail(oid).then(res => {
      this.setData({ orderDetail: res })

      this.transformTime()   // 时间转换
      this.getRoutePlanning()   // 规划路线
    })
  },

  // 时间转换
  transformTime () {
    let time = this.data.orderDetail.posttime
    let postTime = MyDate(time, 0)
    this.setData({ postTime })
  },

  // 路线规划
  getRoutePlanning () {
    let myLocation = wx.getStorageSync('myLocation')
    let toLng = myLocation.longitude
    let toLat = myLocation.latitude

    let fromlat = this.data.orderDetail.lat
    let fromlng = this.data.orderDetail.lng

    let from = fromlat + ',' + fromlng
    let to = toLat + ',' + toLng

    user.getRoutePlanning(from, to).then(res => {
      let polyline = this.data.polyline
      polyline[0]['points'] = res.polyline
      let distance = (res.distance / 1000).toFixed(1)
      this.setData({
        polyline, distance,
        duration: res.duration
      })
    })

  },
})